<template>
  <section>


    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="query">
        <el-form-item>
          <el-input v-model="query.keyword" placeholder="员工姓名"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="getQueryEmployees">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </el-col>
    <!--列表-->
    <el-table :data="this.pageInfo.dataList" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">

      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="60">
      </el-table-column>

      <el-table-column prop="sn" label="订单编号">
      </el-table-column>

      <el-table-column prop="productName" label="产品名">
      </el-table-column>

      <el-table-column prop="price" label="订单价格">
      </el-table-column>

      <el-table-column prop="customer" label="客户名">
      </el-table-column>
      <el-table-column prop="state" label="状态">
        <template slot-scope="scope">
          <span v-if="scope.row.state==0" style="color: #18c79c">待付尾款</span>
          <span v-if="scope.row.state==1" style="color: #20a0ff">已成交</span>


        </template>
      </el-table-column>

      <el-table-column label="操作" width="400">
        <template scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button size="small" @click="handlePayment(scope.$index, scope.row)">付尾款</el-button>
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">退款</el-button>
          <!--					<el-button type="primary" size="small" @click="handleRole(scope.$index, scope.row)">编辑产品</el-button>-->
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">移入商机池</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <!--	<el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="this.query.pageSize" :total="this.pageInfo.total" style="float:right;">
        </el-pagination>-->
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page.sync="this.query.currentPage"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="10"
          layout="total, sizes, prev, pager, next, jumper"
          style="float:right;"
          :total="this.pageInfo.total">
      </el-pagination>
    </el-col>


    <!--新增/添加界面-->
    <el-dialog title="新增/添加界面" :visible.sync="paymentFormVisible" :close-on-click-modal="false">
      <el-form :model="saveFrom" label-width="80px"  ref="saveFrom">
        <el-form-item label="key" prop="key">
          <el-input v-model="saveFrom.key" auto-complete="off"></el-input>
        </el-form-item>

        <el-form-item label="value" prop="value">
          <el-input v-model="saveFrom.value"></el-input>
        </el-form-item>

        <el-form-item label="type" prop="type">
          <el-input v-model="saveFrom.type"></el-input>
        </el-form-item>

        <el-form-item label="intro" prop="intro">
          <el-input v-model="saveFrom.intro" ></el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="saveFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
      </div>
    </el-dialog>


    <!--付尾款界面-->
    <el-dialog title="付尾款界面" :visible.sync="paymentFormVisible" :close-on-click-modal="false">
      <el-form :model="paymentFrom" label-width="80px"  ref="paymentFrom">
        <el-form-item label="订单号" prop="sn">
          <el-input v-model="paymentFrom.sn" disabled auto-complete="off"></el-input>
        </el-form-item>

        <el-form-item label="产品名称" prop="productName">
          <el-input v-model="paymentFrom.productName" disabled></el-input>
        </el-form-item>

        <el-form-item label="待付金额" prop="price">
          <el-input v-model="paymentFrom.price" disabled></el-input>
        </el-form-item>


        <el-form-item label="支付方式">
          <el-select v-model="paymentFrom.paymentMethod" value-key="id" placeholder="请选择付款方式">
            <el-option
                v-for="status in paymentMethods"
                :key="status.id"
                :label="status.title"
                :value="status">
              <span> {{status.title}}</span>
            </el-option>
          </el-select>
        </el-form-item>


      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="paymentFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="addPayment" :loading="addLoading">提交</el-button>
      </div>
    </el-dialog>


  </section>
</template>

<script>



export default {
  data() {
    return {
      //----------订单付尾款

      //显示
      paymentFormVisible:false,

      paymentFrom:{
        sn:"",
        productName:"",
        price:"",
        paymentMethod:{
          id:"",
          title:""
        }
      },
      paymentMethods:[], //付款方式

// ---------------------------
      checkAll: false,
      checkedRoles: [],
      roleList: [],			  //产品列表
      isIndeterminate: true,
      allChecked:[],  //全选时候的id



      employeeRole:{
        employeeId:null,
        roleIds:[] // 用户的产品集合
      },



      //全部部门的数组
      departmentList:[],


      query: {
        keyword: '',
        currentPage:1,
        pageSize:10
      },
      pageInfo:{
        dataList: [],
        total: 0
      },
      page: 1,
      listLoading: false,
      sels: [],//列表选中列

      editFormVisible: false,//编辑界面是否显示
      editLoading: false,
      editFormRules: {
        name: [
          { required: true, message: '请输入姓名', trigger: 'blur' }
        ]
      },

      saveFormVisible: false,//新增界面是否显示
      addLoading: false,
      saveFromRules: {
        name: [
          { required: true, message: '请输入姓名', trigger: 'blur' }
        ]
      },

      //新增界面数据
      saveFrom: {
        id:null,
        key:"",
        value:"",
        type:"",
        intro:""
      },
      //产品列表
      roles:[],

      employees:[],
      //
      deptTree:[],
      deptFrom: {
        id:null,
        name: "",
        intro:"",
        manager:{
          id:null,
          username:""
        },
        parent:{
          id:null,
          name:""
        },
        state:""
      },
    }
  },
  methods: {
    // 提交
    addPayment(){
      this.$refs.paymentFrom.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            this.addLoading = true;

            let para = Object.assign({}, this.paymentFrom);
            console.log(para);

            this.$http.post("/order/addPaymentr",para)
                .then(result=>{
                  result = result.data
                  if (result.success){
                    this.$message({message: '保存成功', type: 'success' });
                    this.addLoading = false;
                    this.paymentFormVisible=false; //成功关闭窗口
                    this.query.currentPage = 1;
                    // 刷新列表
                    this.getOrders()
                  }else{
                    this.$message({message: '保存失败', type: 'danger' });
                    this.addLoading = false;
                  }
                })
                .catch(result=>{
                  this.$message({message: '网络故障', type: 'danger' });
                  this.addLoading = false;
                })
          })
              .catch(() => {
                this.$message({message: '网络故障', type: 'danger' });
                this.addLoading = false;
              });

        }
      });
    },


    //显示
    handlePayment(index,row){
      this.paymentFrom=row
      this.getPaymentMethods()
      console.log(row.id)
      this.getPaymentPrice(row.id) //id是订单id
      console.log("----"+this.paymentFrom.price)
      this.paymentFormVisible=true
    },

    // 获取尾款
    getPaymentPrice(id){
      this.$http.get("/order/getPaymentPrice/"+id)
          .then(result => {
            result = result.data;
            if(result.success){
              this.paymentFrom.price = result.resultObj;
            }else{
              this.$message({ message: result.message,type: 'error'});
            }
          }).catch(result => {

      })
    },


// 获取付款方式

    getPaymentMethods(){
      this.$http.get("/dictionaryitem/dictionary/6")
          .then(result => {
            result = result.data;
            if(result.success){
              this.paymentMethods = result.resultObj;
            }else{
              this.$message({ message: result.message,type: 'error'});
            }
          }).catch(result => {
        this.$message({ message: '获取付款列表错误',type: 'error'});
      })
    },

    //全选
    handleCheckAllChange(val) {
      this.checkedRoles = val ? this.allChecked : [];
      this.isIndeterminate = false;
    },
    handleCheckedCitiesChange(value) {
      let checkedCount = value.length;
      this.checkAll = checkedCount === this.roleList.length;
      this.isIndeterminate = checkedCount > 0 && checkedCount < this.roleList.length;
    },

    //显示
    handleRole: function (index, row) {

      this.getRoleList()
      this.employeeRole.employeeId= row.id


    },

    // 获取产品列表的方法
    getRoleList(){
      this.$http.get("/role")
          .then(result => {
            result = result.data;
            if(result.success){
              this.roleList = result.resultObj;
              console.log("roleList:"+this.roleList)
              for (let i = 0; i < this.roleList.length; i++) {
                let p=this.roleList[i]
                this.allChecked.push(p.id)
              }
            }else{
              this.$message({ message: result.message,type: 'error'});
            }
          }).catch(result => {
        this.$message({ message: '获取产品列表错误',type: 'error'});
      })
    },


    //--------------------------------------------------------------
    handleSizeChange(val){
      this.query.pageSize = val;
      this.getOrders()
    },
    // 点击查询按钮
    getQueryEmployees(){
      this.query.currentPage = 1
      this.getOrders()
    },
    //性别显示转换
    formatSex: function (row, column) {
      return row.sex == 1 ? '男' : row.sex == 0 ? '女' : '未知';
    },
    handleCurrentChange(val) {
      this.query.currentPage = val;
      this.getOrders()
    },

    //获取全部配置列表
    getOrders() {
      // No 'Access-Control-Allow-Origin'
      // 只要看到浏览器报这个错误就是跨域问题 前后端分离的项目一定会有跨越问题
      this.listLoading = true;
      // 发请求
      // localhost:8080/employee  全局的前缀
      this.$http.put("/order",this.query)
          .then(result=>{
            result = result.data
            if (result.success){
              this.$message({message: '查询成功', type: 'success' });
              this.listLoading = false;
              this.pageInfo = result.resultObj
              console.log(this.pageInfo.dataList);
            }else{
              this.$message({message: '查询失败', type: 'danger' });
            }
          })
          .catch(result=>{

          })

    },

    //删除
    handleDel: function (index, row) {
      this.$confirm('确认删除该记录吗?', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true; // 模态框
        //NProgress.start();
        let para = { id: row.id };
        //发请求
        this.$http.delete("/order/"+row.id)
            .then(result=>{
              result = result.data
              if (result.success){
                this.$message({message: '删除成功', type: 'success' });
                this.listLoading = false;
                this.query.currentPage = 1;
                // 刷新列表
                this.getOrders()
              }else{
                this.$message({message: '删除失败', type: 'danger' });
              }
            })
      }).catch(() => {

      });
    },

    //显示编辑界面
    handleEdit: function (index, row) {
      this.saveFormVisible = true;
      this.saveFrom= Object.assign({}, row);  //赋值

      this.getOrders()

    },

    //显示新增界面
    handleAdd: function () {
      this.saveFormVisible = true;
      this.getOrders()

      this.saveFrom = {
        id:null,
        key:"",
        value:"",
        type:"",
        intro:""
      };

    },
    //编辑

    ///-----------------------------------------------------
    //新增  / 修改
    addSubmit: function () {
      this.$refs.saveFrom.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            this.addLoading = true;
            //NProgress.start();
            let para = Object.assign({}, this.saveFrom);
            console.log(para);
            
            this.$http.post("/order",para)
                .then(result=>{
                  result = result.data
                  if (result.success){
                    this.$message({message: '保存成功', type: 'success' });
                    this.addLoading = false;
                    this.saveFormVisible=false; //成功关闭窗口
                    this.query.currentPage = 1;
                    // 刷新列表
                    this.getOrders()
                  }else{
                    this.$message({message: '保存失败', type: 'danger' });
                    this.addLoading = false;
                  }
                })
                .catch(result=>{
                  this.$message({message: '网络故障', type: 'danger' });
                  this.addLoading = false;
                })
          })
              .catch(() => {
                this.$message({message: '网络故障', type: 'danger' });
                this.addLoading = false;
              });

        }
      });
    },
    selsChange: function (sels) {
      this.sels = sels;
    },

    //批量删除
    batchRemove: function () {
      var ids = this.sels.map(item => item.id);
      this.$confirm('确认删除选中记录吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        // 发请求
        this.$http.patch("/order",ids)
            .then(result=>{
              result = result.data
              if (result.success){
                this.$message({message: '批量删除成功', type: 'success' });
                this.listLoading = false;
                this.query.currentPage = 1;
                // 刷新列表
                this.getOrders()
              }else{
                this.$message({message: '批量删除失败', type: 'danger' });
              }
            })
            .catch(result=>{

            })
      })
          .catch(() => {

          });
    },


  },
  mounted() {
    this.getOrders();

  }
}

</script>

<style scoped>

</style>